package com.wtwd.device.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.device.constant.RespCode;
import com.wtwd.device.dao.AppManagementMapper;
import com.wtwd.device.dao.AppVersionMapper;
import com.wtwd.device.model.entity.AppManagement;
import com.wtwd.device.model.vo.AppNameVo;
import com.wtwd.device.model.vo.RespEntity;
import com.wtwd.device.model.vo.ResultVO;
import com.wtwd.device.service.AppManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author mjy
 * @date 2021/11/08
 * @description
 */
@Service
public class AppManagementServiceImpl implements AppManagementService {

    @Autowired
    private AppManagementMapper appManagementMapper;

    @Autowired
    private AppVersionMapper appVersionMapper;

    @Override
    public IPage<AppManagement> getPagingAppManagementList(Page<Object> page) {
        return appManagementMapper.getPagingAppManagementList(page);
    }

    @Override
    public AppManagement getAppManagementByName(String appName) {
        return appManagementMapper.getAppManagementByName(appName);
    }

    @Override
    public Integer addApp(AppManagement appManagement) {
        return appManagementMapper.addApp(appManagement);
    }

    @Override
    public RespEntity updateApp(AppManagement appManagement) {
        Integer rest = appManagementMapper.updateApp(appManagement);
        return new RespEntity(rest > 0 ? RespCode.SUCCESS : RespCode.FAILED);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public RespEntity deleteAppById(long id) {
        Integer rest = appManagementMapper.deleteAppById(id);
        //删除app时清除该app的版本控制
        appVersionMapper.deleteAppVersionByAppId(id);
        return new RespEntity(rest > 0 ? RespCode.SUCCESS : RespCode.FAILED);
    }

    @Override
    public ResultVO<List<AppNameVo>> getAllAppName() {
        List<AppNameVo> appNameVos = appManagementMapper.getAllAppName();
        return ResultVO.success(RespCode.SUCCESS,appNameVos);
    }
    

   
}
